<%@ page language="java" %>
<%
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("max-age", 0);
    response.setDateHeader("Expires", 0);
%>

<%@ page import="com.scalar.chiptrack.utils.ConfigManager,
                 com.scalar.chiptrack.users.User,
                 com.scalar.chiptrack.commons.SessionKeys"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>

<%String chipTracContext = "/" + ConfigManager.getChipTracContext();%>


<html>

<HEAD>
	<TITLE>ScalarSoft - ChipTrac - Wafer Data Entry </TITLE>
    <META http-equiv='Content-Type' content='text/html; charset=windows-1252'>
    <LINK href='<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css' type='text/css' rel='stylesheet'>
	<script src="<%=chipTracContext%>/jsp/includes/scripts/validations.js"></script>
    <script src="<%=chipTracContext%>/jsp/includes/scripts/functions.js"></script>
    <script language='javascript' >

	function resetValues()
    {
        document.waferDataEntryForm.reset();
        onLoad();
    }

    function save()
	{
        var rowIndex = <%=request.getParameter("rowIndex")%>;
        var pa  = document.getElementById("message");
        pa.innerHTML = '';

        var form		            = document.waferDataEntryForm;
		var openerForm	            = window.opener.document.forms[0];

        var openerFormwaferIdCtrl   = eval( openerForm['wipLotsList['+ rowIndex+'].waferIds'] );
        var checkedWaferIdsCtrl     = openerForm["wipLotsList[" + rowIndex +"].checkedWaferIdsAsString"];
        var waferIdsValueStrCtrl    = openerForm["wipLotsList[" + rowIndex +"].waferIdValueAsString"];
        var openerFormShipQtyCtrl   = eval(openerForm['wipLotsList['+ rowIndex+'].shipQty'] );
        var openerFormRemainQtyCtrl = eval(openerForm['wipLotsList['+ rowIndex+'].remainQty'] );
        var openerFormshipQty  = eval(openerForm['wipLotsList['+ rowIndex+'].waferQtyIn'].value );

        var waferIdsValue           = openerFormwaferIdCtrl.value;

        if ( eval(waferIdsValue.lastIndexOf(";") + 1 ) == waferIdsValue.length )
        {
             waferIdsValue = waferIdsValue.substring(0, eval(waferIdsValue.length-1));
        }

        var waferIdsArray      = waferIdsValue.split(";");

        var waferIdCtrl        =  form.waferId;
        var checkedWaferIdsStr  = '';

        if ( waferIdCtrl.length != null )
        {
            var waferIds = "";
            var lengthOfSelectedWaferIds = 0;

            for ( i = 0; i < waferIdCtrl.length; i++ )
            {
                if( waferIdCtrl[i].checked == true )
                {
                    lengthOfSelectedWaferIds++;
                    checkedWaferIdsStr      = (checkedWaferIdsStr != '' ? checkedWaferIdsStr + ";" + waferIdCtrl[i].value : waferIdCtrl[i].value);
                }
            }

            if ( (waferIdsValue == null || waferIdsValue == '' ) && eval(lengthOfSelectedWaferIds) == 0 )
            {
                 pa.innerHTML = "<li>Wafers Info not available; Please select Wafers</li>";
                 return;
            }
            if( eval(lengthOfSelectedWaferIds) == 0 )
            {
                pa.innerHTML = "<li>Please select Wafers</li>";
                return;
            }
            if( eval(openerFormshipQty) < eval(lengthOfSelectedWaferIds) )
            {
                pa.innerHTML = "<li>Wafers selected should not be more than the Ship Qty <font color='blue'> "+ eval(openerFormshipQty) + " </font></li>";
                return;
            }
            //START: ASSIGNING WAFER IDS TO THE PARENT WINDOW
            var isSameWaferIds      = false;

            if (waferIdsValue != null && waferIdsValue != '' )
            {
                for ( var i = 0; i < waferIdCtrl.length; i++ )
                {
                    if( waferIdCtrl[i].checked == true )
                    {
                        for (var k =0; k < waferIdsArray.length; k++)
                        {
                             if ( waferIdCtrl[i].value == waferIdsArray[k] )
                             {
                                 isSameWaferIds = true;
                                 break;
                             }
                             else
                             {
                                 isSameWaferIds = false;
                             }
                        }
                    }
                    else
                    {
                        for (var k =0; k < waferIdsArray.length; k++)
                        {
                             if ( waferIdCtrl[i].value == waferIdsArray[k] )
                             {
                                 break;
                             }
                        }
                      }
                }
            }
            else
            {
                isSameWaferIds = true;
            }

            if ( isSameWaferIds == false)
            {
                pa.innerHTML = "<li> Please select wafers from available wafer ids : " + waferIdsValue;
                return;
            }

            checkedWaferIdsCtrl.value = checkedWaferIdsStr;
            waferIdsValueStrCtrl.value =   checkedWaferIdsStr;
            openerFormShipQtyCtrl.value = eval(lengthOfSelectedWaferIds);
            if (openerForm.baseprocessType.value.toUpperCase() == 'Assembly'.toUpperCase() || openerForm.baseprocessType.value.toUpperCase() == 'Test'.toUpperCase()
                || openerForm.baseprocessType.value.toUpperCase() == 'Class Store'.toUpperCase() || openerForm.baseprocessType.value.toUpperCase() == 'Finished Goods'.toUpperCase()    )
            {
                openerFormRemainQtyCtrl.value = ((+openerFormshipQty)- eval((+lengthOfSelectedWaferIds)) );
            }

        }
        closeWindow();
    }

    function closeWindow()
    {
        window.close();
    }

    function onLoad()
    {
        var rowIndex = <%=request.getParameter("rowIndex")%>;
        var pa  = document.getElementById("message");

        var form		            = document.waferDataEntryForm;
		var openerForm	            = window.opener.document.forms[0];
        var waferIdCtrl             = form.waferId;
        var openerFormWaferIdsCtrl  = eval( openerForm['wipLotsList['+rowIndex+'].waferIds'] );
        var waferIdsValueAsString = openerForm["wipLotsList[" + rowIndex +"].waferIdValueAsString"].value;
        var openerFormWaferIdsValue = openerFormWaferIdsCtrl.value;

        var enteredWaferIdsArr = null;

        if ( waferIdsValueAsString != null && waferIdsValueAsString != '' )
        {
              enteredWaferIdsArr = waferIdsValueAsString.split(";");
        }
        else
        {
            if ( openerFormWaferIdsValue == null || openerFormWaferIdsValue == '' )
            {
                 pa.innerHTML = "<li>WaferId Info not available; Please select Wafers</li>";
                 return;
            }

            enteredWaferIdsArr = openerFormWaferIdsValue.split(";");
        }
        //var enteredWaferIdsArr      = openerFormWaferIdsValue.split(";");
        //START: TO POPULATE ALREADY ENTERED VALUES
        if ( waferIdCtrl != null )
        {
            if ( waferIdCtrl.length )
            {
                var waferIdsCount           = waferIdCtrl.length;
                var enteredwaferIdsCount    = enteredWaferIdsArr.length;
                var minCount                = ( waferIdsCount > enteredwaferIdsCount ) ? enteredwaferIdsCount : waferIdsCount;

                for ( i = 0; i < waferIdsCount; i++ )
                {
                    for ( j = 0; j < minCount; j++ )
                    {
                        if ( waferIdCtrl[i].value == enteredWaferIdsArr[j] )//if( eval( enteredWaferIdsArr[j] -1 ) == i )
                        {
                            waferIdCtrl[i].checked = true;
                        }
                    }
                }

            }
        }

        //END
    }

    /*To select all the checkboxs and deselect the all checkboxes*/
    function setSelectAllWaferIds(Object)
    {
        var selId = document.waferDataEntryForm.waferId;
        var count = 0;
        var pa  = document.getElementById("message");

        if (typeof Object.length =='undefined')
        {
           pa.innerHTML = '';
           if( Object.checked == true )
           {
               document.waferDataEntryForm.waferId.checked = true;
               for ( count = 0; count < selId.length; count ++ )
               {
                    selId[count].checked = true;
               }
            }
            else
            {
               document.waferDataEntryForm.waferId.checked = false;
               for ( count = 0; count < selId.length; count ++ )
               {
                    selId[count].checked = false;
               }
            }
        }
    }

    /*if deselect the checkbox, to deselect the SelectAll checkbox*/
    function deSelectWaferIds()
    {
        var pa  = document.getElementById("message");
        pa.innerHTML = '';
        var selId = document.waferDataEntryForm.waferId;
        var selAll = document.waferDataEntryForm.waferIds;

        for( var i=0; i< selId.length;i++ )
        {
            if(selId[i].checked+""=='false')
            {
                document.waferDataEntryForm.waferIds.checked = false;
                return;
            }
            if(selId[i].checked+""=='true')
            {
                document.waferDataEntryForm.waferIds.checked = true;
            }
        }
    }

    </script>
</HEAD>
<%
    User user = ( User ) session.getAttribute ( SessionKeys.USER_KEY );
    if ( user != null )
    {
%>
<BODY bgcolor='ffffff' marginheight='0' marginwidth='0' topmargin='0' leftmargin='0' onLoad='onLoad()'>

    <TABLE align='center' border='0' width='100%' >
        <TR>
            <td align='center' class="NArialW">Wafer Data Entry</td>                                
        </TR>
        <TR>
            <TD align='center'>&nbsp;</TD>
        </TR>
        <form name="waferDataEntryForm" action="">
        <%
            String type =  request.getParameter( "type" ) ;
            int waferQty = 0;
            try
            {
                waferQty = Integer.parseInt( request.getParameter( "waferQty" ) );
            }
            catch( Exception ex )
            {
                waferQty = 0;
            }

            if( waferQty == 0 )
            {
        %>
        <TR>
            <TD align='center'><font class="NArialL" color="red">Wafer Qty Should be Greater zero / Not Entry </font></TD>
        </TR>
        <TR>
            <TD align='center'><font class="NArialL">&nbsp;</font></TD>
        </TR>
        <TR>
            <TD align='center'><a href="javascript:close()" title='Click here to close the window'><img border="0" src="<%=chipTracContext%>/jsp/includes/images/cancel.gif" ></a></TD>
        </TR>
        <%
            }
            else
            {
        %>
         <TR>
           <TD class="NArialL">
           <input type="checkbox" name="waferIds" styleClass="txfield" onClick="javascript:setSelectAllWaferIds(this)"/>
           <font class="NArialL">Select All &nbsp;</font>

           </TD>
         </TR>

        <TR>
            <TD align='center'>
			    <TABLE align="center" border="0" width="100%">
				    <TR>
				        <TD width="100%" colspan='5' class="NArialL">&nbsp;<FONT id="message" size=1 color='FF0000'></FONT></TD>
				    </TR>
            <%
                    for( int count = 0; count < 25 ; count++ )
                    {
            %>
            <%          if( count % 5 == 0 ) { %> <TR> <% } %>
                        <TD  class="NArialL">
                            <input type="checkbox" name="waferId" value = "<%=count+1%>" styleClass="txfield" onClick="javascript:deSelectWaferIds()"/>&nbsp;Wafer-<%=count+1%>&nbsp;&nbsp;&nbsp;
                        </TD>
            <%
                    }
            %>
                </TABLE>
            </TD>
        </TR>
        <TR><TD align='center'>&nbsp;</TD></TR>
         <%
             if ( type != null && type.equalsIgnoreCase("view"))
             {
         %>
         <TR><TD align='center'><a href="javascript:window.close()" title='Click here to close the window'><img border="0" src="<%=chipTracContext%>/jsp/includes/images/cancel.gif" ></a></TD></TR>
         <% }
         else
         { %>
        <TR>
            <TD align='center'>
                    <a href="javascript:save()" title='Click here to Save Wafer Data Entry'><img border="0" src="<%=chipTracContext%>/jsp/includes/images/save.gif" ></a>&nbsp;&nbsp;&nbsp;
                    <a href="javascript:resetValues()" title='Click here to reset all field values'><img border="0" src="<%=chipTracContext%>/jsp/includes/images/reset.gif" ></a>&nbsp;&nbsp;&nbsp;
                    <a href="javascript:window.close()" title='Click here to close the window'><img border="0" src="<%=chipTracContext%>/jsp/includes/images/cancel.gif" ></a>
            </TD>
        </TR>

    <%   }
      }
    %>
        </form>
    </table>
</body>
<%
    }
    else
    {
%>
    <BODY bgcolor='ffffff' marginheight='0' marginwidth='0' topmargin='0' leftmargin='0' >
    <TABLE cellpadding='0' cellspacing='0' bgcolor='FDF5E6' border="0" width="100%"  height="100%" align=center>
        <TR>
            <TD>
                <TABLE align="center" border="0" width="40%" bgcolor="#FDF5E6">
                    <TR><TD>&nbsp;</TD></TR>
                    <TR><TD align="center" ><font class="NArialL" ><font color="red"><b>Session has been time out , Please close this window and login again.</b></font></font></TD></TR>
                    <TR><TD>&nbsp;</TD></TR>
                    <TR><TD align="center" ><a href="javascript:window.close();"><font class='NArialL'>[Close this window]</font></a></TD></TR>
                </TABLE>
            </TD>
       </TR>
       <TR><TD>&nbsp;</TD></TR>
    </TABLE>
    </BODY>
<% } %>
</html>
